package com.usian.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.usian.common.dto.CompetitionDTO;
import com.usian.common.model.competition.CompetitionHistory;
import com.usian.common.model.competition.CompetitionMember;
import com.usian.common.untils.Result;
import com.usian.common.vo.CompetitionPaperVO;
import com.usian.common.vo.CompetitionVO;
import com.usian.common.vo.PaperVO;
import com.usian.service.CompetitionFeignService;
import com.usian.service.CompetitionHistoryService;
import com.usian.service.PaperFeignService;
import com.usian.service.TrainingFeignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @projectName: 2210A-project1
 * @package: com.usian.controller
 * @className: CompetitionController
 * @author: 莫洪维
 * @email: 1204315800@qq.com
 * @description: TODO
 * @date: 2023/8/28 16:18
 */
@RestController
@RequestMapping("/competition")
public class CompetitionController {

    @Autowired
    private CompetitionFeignService competitionFeignService;
    @Autowired
    private PaperFeignService paperFeignService;
    @Autowired
    private TrainingFeignService trainingFeignService;
    @Autowired
    private CompetitionHistoryService competitionHistoryService;

    /**
     * 删除历史记录方法
     */
    @RequestMapping(value = "/delHistory",method = RequestMethod.DELETE)
    public Result delHistory(CompetitionHistory competitionHistory){
        //调用方法进行删除
        QueryWrapper<CompetitionHistory> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(CompetitionHistory::getCompetitionId,competitionHistory.getCompetitionId())
                .eq(CompetitionHistory::getMemberId,competitionHistory.getMemberId());
        competitionHistoryService.remove(wrapper);
        return Result.ok().message("删除成功");
    }

    /**
     * 保存历史记录方法
     */
    @RequestMapping(value = "/saveHistory",method = RequestMethod.POST)
    public Result saveHistory(@RequestBody CompetitionHistory competitionHistory){
        //判断是否有答案
        if (StringUtils.isBlank(competitionHistory.getAnswer())) {
            return Result.error();
        }

        //调用方法进行查询
        QueryWrapper<CompetitionHistory> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(CompetitionHistory::getPaperId, competitionHistory.getPaperId())
                .eq(CompetitionHistory::getCompetitionId, competitionHistory.getCompetitionId())
                .eq(CompetitionHistory::getMemberId, competitionHistory.getMemberId())
                .eq(CompetitionHistory::getQuestionsId, competitionHistory.getQuestionsId());

        CompetitionHistory one = competitionHistoryService.getOne(wrapper);

        //保存历史记录
        boolean b = (one == null) ? competitionHistoryService.save(competitionHistory) : competitionHistoryService.update(competitionHistory, wrapper);

        return b ? Result.ok().data("comHistory", competitionHistory) : Result.error();
    }

    /**
     * 通过试卷id查询完成试题数量
     * @param competitionHistory
     * @return
     */
    @RequestMapping(value = "/toHistoryNum",method = RequestMethod.GET)
    public Result toHistoryNum(CompetitionHistory competitionHistory){
        //调用方法进行查询
        Integer num = competitionHistoryService.toHistoryNum(competitionHistory);
        return Result.ok().message("查询成功").data("historyNum",num);
    }

    /**
     * 通过试卷id查询完成试题(查询历史记录方法)
     * @param competitionHistory
     * @return
     */
    @RequestMapping(value = "/toHistory",method = RequestMethod.GET)
    public Result toQuestionsNum(CompetitionHistory competitionHistory){
        //调用方法进行查询
        List<CompetitionHistory> list = competitionHistoryService.toHistory(competitionHistory);
        return Result.ok().message("查询成功").data("history",list);
    }

    /**
     * 查询所有试题方法
     * @param paperId
     * @return
     */
    @RequestMapping(value = "/findQuestions",method = RequestMethod.GET)
    public Result findQuestions(Long paperId){
        //调用方法进行查询
        Result questions = paperFeignService.findQuestions(paperId);
        return questions;
    }

    /**
     * 根据竞赛ID查询试卷方法
     * @param competitionId
     * @return
     */
    @RequestMapping(value = "/findPaper",method = RequestMethod.GET)
    public Result findPaper(Long competitionId){
        //调用方法进行查询
        Result paper = competitionFeignService.findPaper(competitionId);
        List listObject = (List) paper.getData().get("list");
        if (null != listObject){
            List<CompetitionPaperVO> competitionPaperVOS = BeanUtil.copyToList(listObject, CompetitionPaperVO.class);
            for (int i = 0; i < competitionPaperVOS.size(); i++) {
                CompetitionPaperVO competitionPaperVO = competitionPaperVOS.get(i);
                PaperVO paperVO = competitionPaperVO.getPaperVO();
                //多少人参加
                Integer studyNum = trainingFeignService.paperMemberNum(paperVO.getId());
                paperVO.setStudyNum(studyNum);
                //多少道题
                Integer questionsNum = paperFeignService.paperQuestionsNum(paperVO.getId());
                paperVO.setQuestionsNum(questionsNum);
                competitionPaperVO.setPaperVO(paperVO);
                competitionPaperVOS.set(i,competitionPaperVO);
            }
            return Result.ok().message("查询成功").data("list",competitionPaperVOS);
        }
        return paper;
    }

    /**
     * 参加竞赛方法(添加成员竞赛中间表数据)
     * @param competitionMember
     * @return
     */
    @RequestMapping(value = "/saveCompetitionMember",method = RequestMethod.POST)
    public Result saveCompetitionMember(@RequestBody CompetitionMember competitionMember){
        //调用方法进行添加
        Result result = competitionFeignService.saveCompetitionMember(competitionMember);
        return result;
    }

    /**
     * 根据竞赛id查询关联成员id
     * @param id
     * @return
     */
    @RequestMapping(value = "/findMemberIds",method = RequestMethod.GET)
    public Result findMemberIds(Long id){
        //调用方法进行查询
        return competitionFeignService.findComMem(id);
    }

    /**
     * 根据id查询竞赛方法
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/findCompetitionById",method = RequestMethod.GET)
    public Result findCompetitionById(Long id){
        //调用方法进行查询
        CompetitionDTO competitionDTO = new CompetitionDTO();
        competitionDTO.setPageSize(1000);
        PageInfo<CompetitionVO> pageInfo = competitionFeignService.list(competitionDTO);
        pageInfo.setList(toStudyNum(pageInfo.getList()));
        CompetitionVO vo = pageInfo.getList().stream().filter(i -> i.getId().equals(id)).findAny().get();
        return Result.ok().message("查询成功").data("competition",vo);
    }

    /**
     * 查询所有竞赛方法
     *
     * @param competitionDTO
     * @return
     */
    @RequestMapping(value = "/findAllCompetition",method = RequestMethod.GET)
    public Result list(CompetitionDTO competitionDTO){
        //调用方法进行查询
        PageInfo<CompetitionVO> pageInfo = competitionFeignService.list(competitionDTO);
        pageInfo.setList(toStudyNum(pageInfo.getList()));
        return Result.ok().message("查询成功").data("competitionList",pageInfo);
    }

    /**
     * 根据竞赛id查询参加人数方法
     */
    private List<CompetitionVO> toStudyNum(List<CompetitionVO> list){
        //封装竞赛当前进度
        for (int i = 0; i < list.size(); i++) {
            CompetitionVO vo = list.get(i);
            //调用方法查询该竞赛参加人数
            vo.setStudyNum(competitionFeignService.competitionStudyNum(vo.getId()));
            list.set(i,vo);
        }
        return list;
    }

}
